<?php
$_SESSION['steps'][$_GET['action']] = false;
if($_SESSION['install_id']=='')
{
	displayError('Vous devez autoriser les variables de sessions');
}
if(is_file($_SESSION['pack_path']))
{
	$start = filesize($_SESSION['pack_path']);
	$w = fopen($_SESSION['pack_path'], 'a+');
}
else 
{
	$start = 0;
	$w = fopen($_SESSION['pack_path'], 'w');
}
session_write_close();

$mstart = time();

$current_version = '0.0.0.0';
if(file_exists($current_path.'/install_report.php5'))
{
	include($current_path.'/install_report.php5');
	
	if($install_report['application_name']!=INSTALL_APPLICATION_NAME)
	{
		displayError('Une autre application a déjà été installé dans ce répértoire.');
	}
	$current_version = $install_report['application_version'];
}
$mode = ($current_version!='0.0.0.0'?'update':'install');
$f = fopen(INSTALL_PACKAGE_HOST.'index.php?action=downlad_version&application='.INSTALL_APPLICATION_NAME.'&version='.$_SESSION['pack_version'].'&mode='.$mode.'&start='.$start, 'r');
if(!$f)
{
	displayError('Impossible d\'accéder au package');
}
$last_progree= microtime(true);
while(time()-$mstart<24 && !feof($f))
{
	$str = fread($f, 4098);
	fwrite($w, $str);
	$start+=strlen($str);
	if((microtime(true)-$last_progree)*1000>800)
	{
		session_start();
		$_SESSION['pack_downloaded'] = $start;
		session_write_close();
		$last_progree= microtime(true);
	}
}
$ended = feof($f);
fclose($f);
fclose($w);

if(!$ended)
{
?>
ajax.sendAsync(page+'?action=down_pack&uid='+new Date().getTime(), function(datas)
{
	evalAjax(datas)
})
<?php
	die();
}
?>
clearTimeout(to)
document.getElementById("div_down").style.width = "100%";
document.getElementById("div_down_label").innerHTML = "100%";
document.getElementById('install_content_intro').innerHTML = 'Le package téléchargé est en cours d\'analysé...';
ajax.sendAsync(page+'?action=check_pack&uid='+new Date().getTime(), function(datas)
{
	evalAjax(datas)
})